package luke4fun.wheresmymoney.adapter.list.impl;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import luke4fun.wheresmymoney.R;
import luke4fun.wheresmymoney.adapter.list.BasicAdapter;
import luke4fun.wheresmymoney.bean.CategoryBean;
import luke4fun.wheresmymoney.viewholder.CategoryListViewHolder;


public class CategoryListAdapter extends BasicAdapter<CategoryBean> {
	
	private List<CategoryBean> dataList;
	private LayoutInflater inflater;
	private Context context;

	public CategoryListAdapter(Context context) {
		this.context = context;
		this.inflater = LayoutInflater.from(context);
		dataList = new ArrayList<CategoryBean>();
	}
	
	@Override
	public int getCount() {
		return dataList == null ? 0 : dataList.size();
	}

	@Override
	public Object getItem(int position) {
		return dataList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		CategoryListViewHolder holder;
		
		// set the view holder for reuse
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.category_list, null);
			holder = new CategoryListViewHolder();
			holder.categoryId = (TextView) convertView.findViewById(R.id.category_id);
			holder.categoryColor = (View) convertView.findViewById(R.id.category_color);
			holder.category = (TextView) convertView.findViewById(R.id.category_txt);
			
			convertView.setTag(holder);
		} else {
			holder = (CategoryListViewHolder) convertView.getTag();
		}
		convertView.setBackgroundColor(context.getResources().getColor(R.color.screen_background_holo_light));
		holder.category.setTextAppearance(context, R.style.normalFont);
		
		CategoryBean bean = dataList.get(position);
		holder.categoryId.setText(bean.getId() + "");
		holder.categoryColor.setBackgroundColor(bean.getColor());
		holder.category.setText(bean.getName());
		if (bean.isSelected()) {
			convertView.setBackgroundColor(bean.getColor());
			holder.category.setTextAppearance(context, R.style.selected);
		}
		
		return convertView;
	}

	public void updateList(List<CategoryBean> list) {
		this.dataList = list;
		notifyDataSetChanged();
	}
}
